package com.plantpoint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.plantpoint.entity.vo.WaterVO;
import com.plantpoint.service.PottedService;
import com.plantpoint.dao.PlantEntryDao;
import com.plantpoint.dao.PottedDao;
import com.plantpoint.entity.bean.PageResult;
import com.plantpoint.entity.dto.PottedAddDTO;
import com.plantpoint.entity.dto.PottedUpdateDTO;
import com.plantpoint.entity.pojo.PlantEntry;
import com.plantpoint.entity.pojo.PottedPlant;
import com.plantpoint.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ATSELI
 * @version 1.0
 * @date 2021/4/20 18:05
 */

@Service
public class PottedServiceImpl implements PottedService {
    private final PottedDao pottedDao;
    private final PlantEntryDao plantEntryDao;
    private final IdWorker idWorker;

    public PottedServiceImpl(PottedDao pottedDao, PlantEntryDao plantEntryDao, IdWorker idWorker) {
        this.pottedDao = pottedDao;
        this.plantEntryDao = plantEntryDao;
        this.idWorker = idWorker;
    }

    @Override
    public int add(HttpServletRequest request, PottedAddDTO pottedPlant) {
        String openid = request.getParameter("openid");
        // 强行测试
        openid = "1";
        PottedPlant pp = new PottedPlant();
        BeanUtils.copyProperties(pottedPlant, pp);
        // 判断改用户是否已经存在同名的盆栽
        QueryWrapper<PottedPlant> qW = new QueryWrapper<>();
        PottedPlant plant = pottedDao.selectOne(qW
                .eq("openid", openid)
                .eq("nickname", pottedPlant.getNickname()));
        if (plant != null){
            return -2;
        }
        // 查询是否存在该盆栽所对应的词条
        QueryWrapper<PlantEntry> qW1 = new QueryWrapper<>();
        PlantEntry scientificName = plantEntryDao.selectOne(qW1.like("scientific_name", pottedPlant.getScientificName()));
        // System.out.println("scientific_name =======>" + scientificName);
        if (scientificName == null) {
            //返回-1则表示不存在该词条,需要先创建词条
            return -1;
        } else {
            String entryId = scientificName.getId();
            pp.setPlantEntryId(entryId);
        }
        pp.setOpenid(openid);
        String id = idWorker.nextId() + "";
        pp.setId(id);
        pp.setImagesId(id);
        // 首次浇水次数为0
        pp.setWateringTimes(0);
        //返回1代表存入成功,0代表失败
        return pottedDao.insert(pp);
    }

    @Override
    public PottedPlant query(String id) {
        return pottedDao.selectById(id);
        // QueryWrapper<PottedPlant> qW = new QueryWrapper<>();
        // PottedPlant plant = pottedDao.selectOne(qW.eq("id", id));
        // System.out.println("plant ========================>" + plant)
    }

    @Override
    public int update(PottedUpdateDTO pottedUpdateDTO) {
        PottedPlant plant = new PottedPlant();
        BeanUtils.copyProperties(pottedUpdateDTO, plant);
        int i = pottedDao.updateById(plant);
        return i;
    }

    @Override
    public int del(String id) {
        return pottedDao.deleteById(id);
    }

    @Override
    public PageResult<PottedPlant> list(Integer cp, Integer ps) {
        Page<Object> page = PageHelper.startPage(cp, ps);
        List<PottedPlant> pottedPlants = pottedDao.selectList(null);
        return new PageResult<>(page.getPages(), page.getTotal(), pottedPlants);
    }

    @Override
    public WaterVO watering(String id) {
        PottedPlant plant = pottedDao.selectById(id);
        WaterVO vo = new WaterVO();
        vo.setWateringTimes(plant.getWateringTimes() + 1);
        Long time = System.currentTimeMillis() / 1000;
        vo.setLastWateringTime(time);
        BeanUtils.copyProperties(vo, plant);
        pottedDao.updateById(plant);
        return vo;
    }

}
